
package br.unirio.bsi.dsw.AuctionSite.action;

import flexjson.JSONSerializer;

import org.apache.struts.action.*;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.unirio.bsi.dsw.AuctionSite.beans.loginForm;
import br.unirio.bsi.dsw.AuctionSite.model.dao.GenericDAO;

import br.unirio.bsi.dsw.AuctionSite.beans.estiloForm;
import br.unirio.bsi.dsw.AuctionSite.beans.formBeanE;

public class estiloAction extends Action 
{
  @Override
  public ActionForward execute(ActionMapping mapping, 
                               ActionForm form,
                               HttpServletRequest request, 
                               HttpServletResponse response) throws Exception 
  {      
    // pega a session
    HttpSession session = request.getSession();  
    
    // check logged
    loginForm lf = (loginForm)session.getAttribute("loginForm"); 
    if( lf == null || !lf.isLogged() )
      return null;
    
    // pega instancia do DAO
    GenericDAO gD = GenericDAO.getInstance(getResources(request).getMessage("DB_NAME"));
    
    // copy
    estiloForm eF = (estiloForm)form;

    String sel = eF.getSelectedEstilo();    
    if( sel == null )      
      sel = "0";
    
    gD.retrieveAll( "SELECT * From Estilo WHERE IDSuperEstilo=" + sel, 
                             formBeanE.class, eF.getFieldEstilo().getEstilos() );
        
    String jsonSuperEstilo = new JSONSerializer()
                                 .exclude("class","selectedEstilo")
                                 .serialize( eF.getFieldEstilo().getEstilos() );
    
    jsonSuperEstilo = "({\"estilo\":" + jsonSuperEstilo + "})";
        
    response.getWriter().write( jsonSuperEstilo  );
    response.getWriter().flush();
    
    return null;
  }
}
